package com.ruoyi.web.controller.xxc;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.news.domain.*;
import com.ruoyi.news.service.IXcxAddressService;
import com.ruoyi.news.service.IXcxGoodsOrderService;
import com.ruoyi.news.service.IXcxWalletInfoService;
import com.ruoyi.news.service.IXxcWalletService;
import com.ruoyi.news.service.impl.XcxAddressServiceImpl;
import com.ruoyi.news.utils.PostUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小程序-商城订单Controller
 * 
 * @author ruoyi
 * @date 2024-12-24
 */
@RestController
@RequestMapping("/xxc/order")
public class XcxGoodsOrderController extends BaseController
{
    @Autowired
    private IXcxGoodsOrderService xcxGoodsOrderService;

    @Autowired
    private IXxcWalletService xxcWalletService;

    @Autowired
    private IXcxAddressService xcxAddressService;

    @Autowired
    private IXcxWalletInfoService xcxWalletInfoService;

    /**
     * 查询小程序-商城订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(XcxGoodsOrder xcxGoodsOrder)
    {
        startPage();
        List<XcxGoodsOrder> list = xcxGoodsOrderService.selectXcxGoodsOrderList(xcxGoodsOrder);
        return getDataTable(list);
    }

    /**
     * 导出小程序-商城订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "小程序-商城订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XcxGoodsOrder xcxGoodsOrder)
    {
        List<XcxGoodsOrder> list = xcxGoodsOrderService.selectXcxGoodsOrderList(xcxGoodsOrder);
        ExcelUtil<XcxGoodsOrder> util = new ExcelUtil<XcxGoodsOrder>(XcxGoodsOrder.class);
        util.exportExcel(response, list, "小程序-商城订单数据");
    }

    /**
     * 获取小程序-商城订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(xcxGoodsOrderService.selectXcxGoodsOrderById(id));
    }

    @PostMapping("/getPostInfo")
    public AjaxResult add(@RequestBody XcxGoodsOrder xcxGoodsOrder)
    {
        return success(PostUtils.findPost(xcxGoodsOrder.getPostId()));
    }

    /**
     * 新增小程序-商城订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "小程序-商城订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XcxGoodsOrder xcxGoodsOrder, @AuthenticationPrincipal UserDetails user)
    {
        LoginUser loginUser =  (LoginUser)user;
        SysUser sysUser = loginUser.getUser();
        XxcWallet meXxcWallet =  xxcWalletService.selectXxcWalletByUserId(sysUser.getUserId()+"");
        xcxGoodsOrder.setByUserId(sysUser.getUserId()+"");
        xcxGoodsOrder.setCreateBy(sysUser.getUserId()+"");
        BigDecimal nowPrice = xcxGoodsOrder.getPrice();
        //修改用户地址 - 联系方式 - 联系人
        XcxAddress xcxAddress = new XcxAddress();
        xcxAddress.setId(IdUtils.fastSimpleUUID());
        xcxAddress.setUserId(sysUser.getUserId()+"");
        String[] address = xcxGoodsOrder.getAddress().split("-");
        xcxAddress.setProvince(address[0]);
        xcxAddress.setCity(address[1]);
        xcxAddress.setTown(address[2]);
        xcxAddress.setAddressInfo(xcxGoodsOrder.getAddressInfo());
        xcxAddress.setPhone(xcxGoodsOrder.getCanctUser());
        xcxAddress.setPeople(xcxGoodsOrder.getCanctPhone());
        xcxAddressService.insertXcxAddress(xcxAddress);
        //判断月余额
        if(meXxcWallet.getYuE().compareTo(nowPrice) >-1) {
            XcxGoodsOrder free = xcxGoodsOrderService.insertXcxGoodsOrder(xcxGoodsOrder);
            if(free != null) {
                XcxWalletInfo xcxWalletInfo = new XcxWalletInfo();
                xcxWalletInfo.setGoodsOrder(free);
                xcxWalletInfo.setId(IdUtils.fastSimpleUUID());
                xcxWalletInfo.setUserId(sysUser.getUserId()+"");
                xcxWalletInfo.setType("6");
                xcxWalletInfo.setBid(free.getId());
                return success(xcxWalletInfoService.insertGoodsXcxWalletInfo(xcxWalletInfo,sysUser));
            }else {
                return error("系统异常,请联系客服");
            }
        }else{
            return error("余额不足");
        }

    }

    /**
     * 修改小程序-商城订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "小程序-商城订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XcxGoodsOrder xcxGoodsOrder)
    {
        return toAjax(xcxGoodsOrderService.updateXcxGoodsOrder(xcxGoodsOrder));
    }

    /**
     * 删除小程序-商城订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "小程序-商城订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(xcxGoodsOrderService.deleteXcxGoodsOrderByIds(ids));
    }
}
